from django.shortcuts import render
from rest_framework.authentication import SessionAuthentication
from rest_framework.viewsets import GenericViewSet
from rest_framework.decorators import action
from .models import address
from .serializer import addressSerializer
from rest_framework.permissions import IsAuthenticated
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from buyHigh.utils.buyHighResponse import APIResponse
# Create your views here.
class addressView(GenericViewSet):
    permission_classes = [IsAuthenticated]
    authentication_classes = [JSONWebTokenAuthentication]
    queryset = address.objects.all()
    serializer_class = addressSerializer
    @action(detail=False, methods=['get'])
    def get_address_list(self, request, *args, **kwargs):
        user=request.user
        queryset=address.objects.filter(user=user)
        data=addressSerializer(queryset, many=True).data
        return APIResponse(data=data, status=200, statusCode=200, message='success')
    @action(detail=False, methods=['put'],url_path='update_receipt_address/(?P<pk>\d+)')
    def update_receipt_address(self, request, *args, **kwargs):
        user=request.user
        addressObj=address.objects.get(pk=self.kwargs['pk'],user=user)
        if not addressObj:
            return APIResponse(status=404, statusCode=404, message='地址不存在')
        ser=addressSerializer(data=request.data,instance=addressObj,partial=True)
        ser.context['user']=user
        if ser.is_valid():
            ser.save()
            return APIResponse(data=ser.data, status=200, statusCode=200, message='success',code=200)
        else:
            return APIResponse(data=ser.errors, status=400, statusCode=400, message='fail')
    @action(detail=False, methods=['post'])
    def create_address(self, request, *args, **kwargs):
        ser=addressSerializer(data=request.data)
        user=request.user
        ser.context['user'] = user
        if ser.is_valid():
            ser.save()
            return APIResponse(data=ser.data, status=200, statusCode=200, message='success',code=200)
        else:
            return APIResponse(data=ser.errors, status=400, statusCode=400, message='fail',code=400)
    @action(detail=False, methods=['get'],url_path='get_address_detail/(?P<pk>\d+)')
    def get_address_detail(self, request, *args, **kwargs):
        print('地址的id是')
        print(self.kwargs['pk'])
        user=request.user
        addressObj=address.objects.get(pk=self.kwargs['pk'],user=user)
        if not addressObj:
            return APIResponse(status=404, statusCode=404, message='地址不存在')
        ser=addressSerializer(instance=addressObj)
        return APIResponse(data=ser.data, status=200, statusCode=200, message='success',code=200)

    @action(detail=False, methods=['delete'],url_path='delete_address/(?P<pk>\d+)')
    def delete_address(self, request, *args, **kwargs):
        user=request.user
        addressObj=address.objects.get(pk=self.kwargs['pk'],user=user)
        if not addressObj:
            return APIResponse(status=404, statusCode=404, message='地址不存在',code=404)
        addressObj.delete()
        return APIResponse(status=200, statusCode=200, message='success',code=200)











